<?php

namespace App\Http\Middleware;

use App\Models\Merchant;
use App\Models\Wxuser;
use App\Models\WxuserMcard;
use Closure;

class CheckWxuserMcard
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if(env('APP_ENV')=='local'){//本地开发
            $merchant_id = 470;

            if(empty(session('web_wxuser_mcard'))){
                $pathinfo = $request->getPathInfo();

                if(!in_array($pathinfo,['/bind-phone','/bind-user-info']) && strcasecmp($request->method(),'GET')==0) session(['want_redirect_url'=>$pathinfo]);

                session(['web_auth_user'=>Wxuser::find('2')]);
                session(['web_merchant'=>Merchant::find($merchant_id)]);
                session(['web_wxuser_mcard'=>WxuserMcard::find(27)]);
            }
            return $next($request);

        }
        if(empty(session('web_wxuser_mcard'))){
            $pathinfo = $request->getPathInfo();
            //if(!in_array($pathinfo,['/bind-phone','/bind-user-info']) && strcasecmp($request->method(),'GET')==0) session(['want_redirect_url'=>$pathinfo]);
            session(['want_redirect_url'=>$pathinfo]);

            $merchant_id = $request->route('merchant_id')??env('DEFAULT_MERCHANT_ID',422);

            return redirect()->route('WAauth',['merchant_id'=>$merchant_id]);
        }

        return $next($request);
    }
}
